import { fetchtype, fetchtypedata } from '@/services/flowMine/waitApprovals';
import {
    getCandidateDetail,
    getInterviewRecord
} from '@/services/myRecruit/candidate';

export default {
    namespace: 'applyCommon',
    state: {
        applytype: [],
        statusPropsList: [], // 申请流程状态字典数据
        candiDetail: {},
        interviewList: []
    },
    effects: {
        *fetchType({ payload }, { call, put }) {
            const result = yield call(fetchtype, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        applytype: result.data
                    }
                });
            }
        },
        // 申请类型
        *fetchTypeData({ payload }, { call, put }) {
            const result = yield call(fetchtypedata, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        applytype: result.data
                    }
                });
            }
        },
        // eslint-disable-next-line consistent-return
        *fetchStatuslist({ payload }, { call, put, select }) {
            const statusPropsList = yield select(
                state => state.applyCommon.statusPropsList
            );
            if (Array.isArray(statusPropsList) && statusPropsList.length) {
                return statusPropsList;
            }
            const result = yield call(fetchtype, payload);
            if (result && result.code === 10000) {
                const { data } = result;
                const newData = [];
                data.forEach(item => {
                    newData.push({ key: item.dictValue, value: item.dictName });
                });
                yield put({
                    type: 'save',
                    payload: {
                        statusPropsList: newData
                    }
                });
                return newData;
            }
        },
        // 请求候选人详情信息
        *candidateDetail({ payload }, { call, put }) {
            const result = yield call(getCandidateDetail, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        candiDetail: result.data
                    }
                });
            }
        },
        // 查询候选人筛选/面试记录
        *fetchInterviewRecord({ payload }, { call, put }) {
            const result = yield call(getInterviewRecord, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        interviewList: result.data
                    }
                });
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
